﻿@using ZBJF.Warranty.BLL
@using ZBJF.Warranty.Common.Enums.InsuranceWarranty
@using ZBJF.Warranty.WebProvider
@using EnumHelper = ZBJF.Warranty.Common.Common.EnumHelper
@using ZBJF.Warranty.Common.Enums.InfoCollect
@using ZBJF.Warranty.Model
@model ZBJF.Warranty.Model.InsuranceWarrantyRecord
@{
    ViewBag.Title = "维修中工单详情";
}
@Html.HiddenFor(m => m.ID)
@Html.HiddenFor(m => m.InsuranceOrderId)

@RenderPage("~/Views/WorkOrderCommonInfo/AuditInfo.cshtml", new { Type = "ShowResult", AuditProjectType = (byte)AuditProjectType.RepairCenter })

<h4>维修具体项目</h4>
<form id="formInsuranceWarrantyRecordAccessories" name="formInsuranceWarrantyRecordAccessories">
    <table class="table table-striped table-bordered table-hover" id="tbInsuranceWarrantyRecordAccessories">
        <thead>
            <tr>
                <th>序号</th>
                <th>维修配件</th>
                <th>
                    配件品质
                </th>
                <th>配件价格</th>

            </tr>
        </thead>
        <tbody>
            @* ReSharper disable once InconsistentNaming *@
            @{
                var insuranceWarrantyRecordAccessories = (Model.InsuranceWarrantyRecordAccessories.Any() ? Model.InsuranceWarrantyRecordAccessories : new List<InsuranceWarrantyRecordAccessories>() { new InsuranceWarrantyRecordAccessories() }).ToList();
                for (int i = 0; i < insuranceWarrantyRecordAccessories.Count; i++)
                {
                    <tr class="text-center" id="trInsuranceWarrantyRecordAccessories_@(i)">
                        <td>
                            @(i + 1)
                        </td>
                        <td>
                            @Html.HiddenFor(m => insuranceWarrantyRecordAccessories[i].ID)
                            @Html.HiddenFor(m => insuranceWarrantyRecordAccessories[i].InsuranceWarrantyRecordID)
                            @Html.DropDownListFor(m => insuranceWarrantyRecordAccessories[i].AccessoriesTypeID, ConvertToSelector.Instance.AccessoriesTypeConvertToSelectList(Model.InsuranceOrder.ProductTypeId??0,addEmptySelect: false), new { validate = "{required:true,messages:{required:'请选择维修配件！'}}", onchange = "accessoriesTypeIDChange(this)" })
                        </td>
                        <td>
                            @Html.DropDownListFor(m => insuranceWarrantyRecordAccessories[i].AccessoriesLevelID, ConvertToSelector.Instance.AccessoriesLevelConvertToSelectList(selectedValue: insuranceWarrantyRecordAccessories[i].AccessoriesLevelID), new { validate = "{required:true,messages:{required:'请选择维修配件品质'}}" })
                        </td>
                        <td>
                            @Html.TextBoxFor(m => insuranceWarrantyRecordAccessories[i].AccessoriesPrice, new {validate = "{max:999999.99,number:true,messages:{required:'请填写配件价格'}}", onkeyup = "onlyInputDecimal(this,2)", MaxLength = "9" })
                        </td>
                        @* <td>
                                暂时只允许维修一个配件，后续再开放<a class='Edit-R ' href="javascript:void(0)" title='delete' onclick="removeInsuranceWarrantyRecordAccessories('trInsuranceWarrantyRecordAccessories_@i')"><i class='icon-remove'></i></a>
                                <a><i id="btnAddNewRow" class='icon-plus'></i></a>
                            </td>*@
                    </tr>
                }
            }
        </tbody>

    </table>
</form>

@RenderPage("~/Views/WorkOrderCommonInfo/InfoCollectItems.cshtml", new { CollectWithType = (byte)CollectWithType.CompleteCollect })

@if (Model.WarrantyMaintenanceTypeID == (byte)WarrantyMaintenanceType.OfficialRepair) //自修时才显示
{
    <!--发票信息收集-->
    @RenderPage("~/Views/WorkOrderCommonInfo/InfoCollectItems.cshtml", new { CollectWithType = (byte)CollectWithType.ProofInvoice })
}
<!--通融历史-->
@RenderPage("~/Views/WorkOrderCommonInfo/InsuranceWarrantyBendTheRulesRecordInfo.cshtml")
<!--历史工单问题-->
@RenderPage("~/Views/WorkOrderCommonInfo/InsuranceOrderFeedbackHistory.cshtml")
<h4>寄回物流信息</h4>
@{
    var insuranceWarrantyRecordExpressInfo = Model.InsuranceWarrantyRecordExpressInfo.FirstOrDefault(o => o.ExpressSendByWhoType == (byte)ExpressSendByWhoType.RepairCenterSendToUser) ?? new InsuranceWarrantyRecordExpressInfo();
}
<form id="fromExpressInfo" enctype="multipart/form-data" method="post">
    <input type="hidden" name="insuranceWarrantyRecordID" value="@Model.ID" />
    @Html.HiddenFor(m => m.ID)
    <table class="table table-responsive table-bordered table-hover">
        <tr>
            <th>快递单号：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressNo, new { validate = "{isRightfulStringNounderline:true,required:true,messages:{required:'请填写快递单号'}}" })</td>
            <th>物流状态：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressStatus)</td>
        </tr>
        <tr>
            <th>快递公司：</th>
            <td>@Html.DropDownListFor(n => insuranceWarrantyRecordExpressInfo.ExpressCompanyID, ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ExpressEnums), (byte) ExpressEnums.SF))</td>
            <th class="w120">寄件人：</th>
            <td class="w-p25">@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserName, new {validate = "{required:true,messages:{required:'请填写寄件人'}}"})</td>

        </tr>
        <tr>
            <th class="w120">寄件人电话：</th>
            <td class="w-p25">@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserPhone, new {validate = "{isTelphone:true,required:true,messages:{required:'请填写寄件人电话'}}"})</td>
            <th>收件人：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserName, new {Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.ReceiverUserName) ? Model.Reporter : insuranceWarrantyRecordExpressInfo.ReceiverUserName, validate = "{required:true,messages:{required:'请填写收件人'}}"})</td>
        </tr>
        <tr>
            <th>收件人电话：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserPhone, new { Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.ReceiverUserPhone) ? Model.ReporterPhone : insuranceWarrantyRecordExpressInfo.ReceiverUserPhone, validate = "{isTelphone:true,required:true,messages:{required:'请填写收件人电话'}}" })</td>
            <th>收件人地址：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserAddress, new {Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.ReceiverUserAddress) ? Model.AccidentAddress : insuranceWarrantyRecordExpressInfo.ReceiverUserAddress, validate = "{required:true,messages:{required:'请填写收件人地址'}}"})</td>
        </tr>
    </table>
</form>
@if (ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.GetParentStatusCode(Model.Status) == (byte)InsuranceWarrantyStatus.InMaintain)
{
    <div class="form-actions text-center">
        <input class="btn btn-primary Edit-R" type="button" onclick="Save(1)" value="保存信息">
        <input class="btn btn-primary Edit-R" type="button" onclick="Save(2)" value="完成维修">
        <input class="btn btn-primary Edit-R" type="submit" value="拒赔" onclick="showIWRRefuseLayer()">
        <input class="btn Edit-R" type="button" value="取消报案" id="btnCancel">
        <input class="btn btn-primary Edit-R" type="button" value="重新派单" id="btnSendOrderAgain" onclick="showLayerOpen('重新派单', '/InMaintainWorkOrder/SendOrderAgain/@Model.ID','800px','600px')">
    </div>
}
    @*---------------取消报案--------------*@
    <div id="showCancelReport">
        <table style="margin: 0 auto; line-height: 50px;">
            <tr>
                <th>请选择取消原因：</th>
                <td>@Html.DropDownList("StatusReasonCancel", ConvertToSelector.Instance.StatusReasonConvertToSelectList(true, ((byte)InsuranceWarrantyStatus.Canceled)))</td>
            </tr>
        </table>
    </div>
    <script src="/Scripts/uploadAdapter/jquery.loadscript.js"></script>
    <script src="/Scripts/uploadAdapter/jquery.uploadAdapter.js"></script>
    <script src="/Scripts/WarrantyRecord/warrantyRecord.js"></script>
    <script>
        $(function () {
            $("#showCancelReport").hide();//隐藏div
            //增加行
            $("#btnAddNewRow").click(function () {
                var html = $(document.getElementById("tbInsuranceWarrantyRecordAccessories").rows[1]).html();
                var lastTrId = $("#tbInsuranceWarrantyRecordAccessories tbody tr:last-child").attr("id");
                var newId = parseInt(lastTrId.substring(lastTrId.indexOf('_') + 1));
                var regExpOrderItem = new RegExp("0", "g"); //第一行序号都是0
                html = html.replace(regExpOrderItem, newId + 1);

                html = "<tr id='trInsuranceWarrantyRecordAccessories_" + (newId + 1) + "'>" + html + "</tr>";
                $("#tbInsuranceWarrantyRecordAccessories tbody").append(html);
                $("#tbInsuranceWarrantyRecordAccessories tbody tr:last-child input").val("");
                $("#tbInsuranceWarrantyRecordAccessories tbody tr:last-child td:first").html(newId + 2);
            });
        })
        //删除行
        function removeInsuranceWarrantyRecordAccessories(trInsuranceWarrantyRecordAccessoriesId) {
            if (($("#tbInsuranceWarrantyRecordAccessories tbody tr").length == 1)) {
                layer.alert("最后一行不能删除，至少保留一行！");
                return;
            }
            $("#" + trInsuranceWarrantyRecordAccessoriesId).remove();
            var i = 0;
            $("#tbInsuranceWarrantyRecordAccessories tbody tr").each(function () {
                $(this).find("td:first").html(i + 1);
                $(this).find("td:last").html("<a class='Edit-R ' href=\"javascript:void(0)\" title='delete' onclick=\"removeInsuranceWarrantyRecordAccessories('trInsuranceWarrantyRecordAccessories_" + i + "')\"><i class='icon-remove'></i></a>");
                $(this).attr("id", "trInsuranceWarrantyRecordAccessories_" + i);
                $(this).find("input").each(function () {
                    var regExpID = new RegExp("_\\d\+__", "g"); //替换ID
                    var regExpName = new RegExp("\\[\\d\+\\]", "g"); //替换Name

                    var id = $(this).attr("id");
                    var name = $(this).attr("name");

                    $(this).attr("id", id.replace(regExpID, "_" + i + "__"));
                    $(this).attr("name", name.replace(regExpName, "[" + i + "]"));

                    var clickEvent = $(this).attr("onclick");
                    if (clickEvent && clickEvent != "")
                        $(this).attr("onclick", clickEvent.replace(regExpID, "_" + i + "__"));

                    var onkeyupEvent = $(this).attr("onkeyup");
                    if (onkeyupEvent && onkeyupEvent != "")
                        $(this).attr("onkeyup", onkeyupEvent.replace(regExpID, "_" + i + "__"));
                });
                i++;
            });
        }

        function accessoriesTypeIDChange(obj) {
            if (obj.value == "") {
                $("#" + $(obj).attr("id").replace("AccessoriesTypeID", "AccessoriesLevelID")).html("<option value='0'>请选择</option>");
            } else {
                $.ajax({
                    url: "/Common/GetAccessoriesLevelList?AccessoriesTypeID=" + obj.value,
                    success: function(data) {
                        var html = "<option value=''>请选择</option>";
                        if (data && data.length > 0) {
                            for (var i = 0; i < data.length; i++) {
                                html += "<option value='";
                                html += data[i].Value;
                                html += "'>" + data[i].Text;
                                html += "</option>";
                            }
                        }
                        $("#" + $(obj).attr("id").replace("AccessoriesTypeID", "AccessoriesLevelID")).html(html);
                    }
                });
            }
        }
        //保存信息
        function Save(saveType) {
            var fromExpressInfo = $('#fromExpressInfo').serializeJson();//快递信息
            //var auditInfoForm = $('#auditInfoForm').serializeJson();//审核信息
            var formInsuranceWarrantyRecordAccessories = $('#formInsuranceWarrantyRecordAccessories').serializeJson();//维修详情
            var param = $.extend({}, fromExpressInfo, formInsuranceWarrantyRecordAccessories);//多个表单参数拼接
            param.Status = "@Model.Status";

            if (saveType == 2) {
                //验证信息收集
                if (!$('#formInsuranceWarrantyRecordAccessories').valid()) {
                    layer.alert("请完善维修具体项目！");
                    return;
                }
                var infoCollectComplete = true;
                $(".InfoCollectCanUploadPicCount").each(function () {
                    //需要上传的图片数大于已经上传的图片数则视为没有完成信息收集
                    if ($(this).text() >
                        $("#" + $(this).attr("id").replace("InfoCollectCanUploadPicCount", "InfoCollectHadUploadPicCount"))
                        .text())
                        infoCollectComplete = false;
                });
                if (!infoCollectComplete) {
                    layer.alert("请完成信息收集！");
                    return;
                }
                ////验证审核信息
                //if (!$("#auditInfoForm").valid()) {
                //    layer.alert("请选择审核结果！");
                //    return;
                //}
            }


            layer.load();
            $.ajax({
                url: "/InMaintainWorkOrder/SaveCompleteMaintainInsuranceWarrantyRecord?ID=@Model.ID" + "&saveType=" + saveType,
                type: "Post",
                data: param,
                success: function (data) {
                    layer.alert(data.Message, {
                        end: function () {
                            layer.closeAll();
                            if (data.Success) {
                                layer.load();
                                location.reload();
                            }
                        },
                        yes: function () {
                            layer.closeAll();
                            if (data.Success) {
                                layer.load();
                                location.reload();
                            }
                        }
                    });
                }
            });
        }
        var index;
        //取消报案
        $("#btnCancel").click(function() {
            index = layer.open({
                type: 1,
                title: '请选择取消报案原因', //标题栏
                closeBtn: false,
                area: '600px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                resize: false,
                btn: ['确认', '取消'],
                btnAlign: 'c', //按钮排列
                yes: function(index, layero) {
                    UpdateInsuranceWarrantyRecordStatus();
                },
                moveType: 1, //拖拽模式，0或者1
                content: $('#showCancelReport').show()
            });
        });
        //取消报案点击确定后修改状态
        function UpdateInsuranceWarrantyRecordStatus() {
            var statusReasonId = $("#StatusReasonCancel").val();
            if (!statusReasonId) {
                layer.alert("请选择");
            }
            var stauts = @((byte) InsuranceWarrantyStatus.Canceled);
            $.ajax({
                url: "/ToBeMaintainWorkOrder/UpdateInsuranceWarrantyRecordStatus?ID=" + @Model.ID,
                dataType: "json",
                type: "GET",
                data: { statusReasonId: statusReasonId, status: stauts },
                success: function(data) {
                    if (data.Result) {
                        layer.close(index);
                        layer.alert(data.Message);
                        return;
                    }
                }
            });
        }

        function showIWRRefuseLayer() {
            layer.open({
                type: 2,
                title: '请选择拒赔原因',
                shadeClose: true,
                shade: 0.8,
                area: ['820px', '400px'],
            content: '/ToBeMaintainWorkOrder/IWRRefuse?insuranceWarrantyRecordId=@Model.ID '+'&infoCollectWithType=@((byte)CollectWithType.CompleteCollect)'+'&auditProjectType=@((byte)AuditProjectType.RepairCenter)'
            });
        }
    </script>